package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo pageInfo = strategyDetailService.getList(qo);
        List<Strategy> Strategys = strategyService.selectAll();
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.selectAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("Strategys",Strategys);
        model.addAttribute("strategyCatalogs",strategyCatalogs);
        return "strategyDetail/list";
    }

    @RequestMapping("/getStrategyByStrategyDetailId")
    @ResponseBody
    public Object getStrategyByStrategyDetailId(Long strategyDetailId){
        Strategy strategy = strategyDetailService.getStrategyByStrategyDetailId(strategyDetailId);
        return strategy;
    }
    @RequestMapping("/getContentByStrategyDetailId")
    @ResponseBody
    public Object getContentByStrategyDetailId(Long detailId){
        StrategyContent strategyContent = strategyDetailService.getContentByStrategyDetailId(detailId);
        return strategyContent;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail, MultipartFile pic) {
        JSONResult json = new JSONResult();
        try {
            if (pic != null) {
                String upload = UploadUtil.upload(pic, UploadUtil.IMG_BASE_PATH + "upload");
                strategyDetail.setCoverUrl(upload);
            }
        strategyDetailService.saveOrUpdate(strategyDetail);
        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }

        return json;
    }

}
